﻿using IDal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Dal
{
    public class Base<T> : IBase<T> where T : class
    {
        public Base(MyContext context)
        {
            Context = context;
        }

        public MyContext Context { get; }

        public async Task<int> AddAsync(T t)
        {
            Context.Set<T>().Add(t);
            return await Context.SaveChangesAsync();
        }

        public async Task<int> DeleteAsync(int id)
        {
            var s=Context.Set<T>().Find(id);
            Context.Set<T>().Remove(s);
            return await Context.SaveChangesAsync();
        }

        public IQueryable<T> GetAll()
        {
            //预备查
            return Context.Set<T>().AsQueryable();
        }

        public T GetSingle(int id)
        {
            return Context.Set<T>().Find(id);
        }

        public async Task<int> RangeDeleteAsync(IQueryable<T> values)
        {
            Context.Set<T>().RemoveRange(values);
            return await Context.SaveChangesAsync();
        }

        public async Task<int> UpdateAsync(T t)
        {
            Context.Set<T>().Update(t);
            return await Context.SaveChangesAsync();
        }
    }
}
